package com.woke.addressactivity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Pair;
import android.view.MenuItem;
import android.view.View;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhongcai.online.R;
import com.zhongcai.online.databinding.ActivityBaseListBinding;

public abstract class BaseListActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, XRecyclerView.LoadingListener {

    protected ActivityBaseListBinding mBinding;

    public abstract int getMenuId();//菜单栏

    public abstract boolean OnMenuItemClickListener(MenuItem item);//菜单栏点击事件

    public abstract boolean OnNavigationClickListener();//返回键点击事件,true 拦截,false 默认处理

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_base_list);
        mBinding.toolbar.setTitle("");
        mBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!OnNavigationClickListener()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        finishAfterTransition();
                    } else {
                        finish();
                    }
                }

            }
        });
        if (getMenuId() > 0) {
            mBinding.toolbar.inflateMenu(getMenuId());
            mBinding.toolbar.setOnMenuItemClickListener(this);
        }

    }

    public void initLayoutManagerAndProgressStyle() {
        mBinding.xRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mBinding.xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mBinding.xRecyclerView.setLoadingListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return OnMenuItemClickListener(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else {
            finish();
        }
    }

    public void startToActivity(Intent intent, Pair... sharedElements) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (sharedElements != null) {
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, sharedElements)
                        .toBundle());
            } else {
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this)
                        .toBundle());
            }
        } else {
            startActivity(intent);
        }
    }

    @Override
    public void onRefresh() {
        mBinding.xRecyclerView.refreshComplete();

    }

    @Override
    public void onLoadMore() {
        mBinding.xRecyclerView.loadMoreComplete();
    }
}
